package com.wzcl.app.controller;


import com.wzcl.app.dao.model.CoursePoint;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.CoursePointDto;
import com.wzcl.app.model.vo.CoursePointVo;
import com.wzcl.app.service.CoursePointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/coursePoint")
@Api(value = "课程大纲controller", tags = {"课程大纲接口"})
public class CoursePointController {

    @Autowired
    private CoursePointService service;

    @ApiOperation("添加课程大纲")
    @PostMapping("/add")
    public RVO<CoursePointVo> addCoursePoint(@RequestBody @Valid CoursePointDto coursePointDto){
        CoursePoint coursePoint = service.addCoursePoint(coursePointDto);
        return RVO.success(CoursePointVo.create(coursePoint));
    }


    @ApiOperation("删除课程大纲")
    @PostMapping("/del")
    public RVO<CoursePointVo> delCoursePoint(@RequestBody CoursePointDto coursePointDto){
        CoursePoint coursePoint = service.delCoursePointById(coursePointDto.getId());
        return RVO.success(CoursePointVo.create(coursePoint));
    }


    @ApiOperation("修改课程大纲")
    @PostMapping("/update")
    public RVO<CoursePointVo> updateCoursePoint(@RequestBody @Valid CoursePointDto coursePointDto) {
        CoursePoint coursePoint = service.updateCoursePoint(coursePointDto);
        return RVO.success(CoursePointVo.create(coursePoint));
    }


    @ApiOperation("获取课程大纲")
    @PostMapping("/get")
    public RVO<CoursePointVo> getCoursePoint(@RequestBody CoursePointDto coursePointDto){
        CoursePoint coursePoint = service.getCoursePointById(coursePointDto.getId());
        if(coursePoint !=null){
           return RVO.success(CoursePointVo.create(coursePoint));
        }
        return RVO.success(null);
    }

    @ApiOperation("课程大纲列表list")
    @PostMapping("/getList")
    public RVO<List<CoursePointVo>> getCoursePointList(@RequestBody CoursePointDto coursePointDto){
        List<CoursePoint> list = service.getCoursePointList(coursePointDto);
        if (list != null && list.size() > 0) {
            List<CoursePointVo> collect = list.stream().map(CoursePointVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("课程大纲列表page")
    @PostMapping("/getPage")
    public  RVO<List<CoursePointVo>> getCoursePointPage(@RequestBody CoursePointDto CoursePointDto){
        MyPage<CoursePoint> page = service.getCoursePointPage(CoursePointDto);
        if (page != null && page.getContent().size()>0) {
            List<CoursePointVo> collect = page.getContent().stream().map(CoursePointVo::create).collect(Collectors.toList());
            return RVO.success(collect,page);
        }
        return RVO.success(null);
    }

}
